<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
</body>
<script id="ColorVertexShader" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    uniform float u_PointSize;
    uniform mat4 u_ModelM4;
    uniform mat4 u_PvM4;
    void main(){
        gl_Position = u_PvM4 * u_ModelM4 * a_Position;
        gl_PointSize = u_PointSize;
    }
</script>
<script id="ColorFragmentShader" type="x-shader/x-fragment">
    precision mediump float;
    uniform vec4 u_Color;
    void main(){
        gl_FragColor = u_Color;
    }
</script>
<script type="module">
    import { Matrix4, Vector3, Color, OrthographicCamera, PerspectiveCamera, Spherical } from '../js/three.module.js';
    import { CreateProgram, imgPromise } from '../js/Utils.js';
    import lib from '../js/ThreeLib.js';
    import CameraController from '../js/CameraController.js'
    import Poly from '../js/Poly4.js'
    import Control from '../js/Control.js'

    import Geo from '../js/Poly2/Geo.js'
    import Mat from '../js/Poly2/Mat.js'
    import Obj3D from '../js/Poly2/Obj3D.js'
    import Scene from '../js/Poly2/Scene.js'
    import Square from '../js/Square.js'



    //#region gl
    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight - 5;

    const gl = canvas.getContext("webgl", { antialias: true, });

    // gl.enable(gl.DEPTH_TEST);//开始背景遮挡会频闪
    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);//去图片描边
    //#endregion



    const scene = new Scene({ gl: gl });
    scene.regProgram('color', {
        program: CreateProgram(gl,
            document.querySelector('#ColorVertexShader').innerText,
            document.querySelector('#ColorFragmentShader').innerText),
        attribs: ['a_Position'],
        uniforms: ['u_PvM4', 'u_ModelM4', 'u_PointSize', 'u_Color']
    });


    const eye = new Vector3(0, 3, 0);
    const lookAt = new Vector3(0, 0, -0.5);

    // const eye = new Vector3(0, 0.5, 3);
    // const lookAt = new Vector3(0, 0, 0);
    const pvM4 = new Matrix4();
    const modelM4 = new Matrix4();
    const camera = new PerspectiveCamera(45, canvas.width / canvas.height, 1, 20);
    const cameraController = new CameraController({
        canvas,
        camera,
        pvMatrix: pvM4,
        eye,
        lookAt,
    });


    const square01 = new Square({ isFace: false, modelM4: new Matrix4().setPosition(0, 0, -0.5) });
    const square02 = new Square({ isFace: false, modelM4: new Matrix4().setPosition(0, 0, 0.3) });
    scene.add(square01)
    scene.add(square02)



    scene.add(createAxis());

    scene.draw();



    function getVOfY(eye, look, j) {
        let a = look.clone();
        let b = look.clone().sub(eye);

        //去Y轴
        a.y = 0;
        b.y = 0;

        let l = b.clone().distanceTo(a);
        console.log(a, b)
    }




    function createAxis() {
        return new Obj3D().createAxis(gl, 'color');
    }




    (function ani() {
        scene.setUniform('u_PvM4', { value: pvM4.elements });
        scene.draw();

        const rotEye = eye.clone().sub(lookAt)
            .applyMatrix4(new Matrix4().makeRotationY(1 * Math.PI / 180))
            .add(lookAt);

        cameraController.setEye(rotEye);

        requestAnimationFrame(ani);
    })();

</script>

</html>